#ifndef __SOUNDMANAGER_H
#define __SOUNDMANAGER_H

#include <string>
#include <map>
#include <vector>
#include "al.h"


///-----------  DEFINICION DE NUEVOS TIPOS ------------------//
	typedef struct
	{
		ALuint	source;
		bool	reserved;
	}InfoSource;
	
	typedef std::string	Action;
	typedef ALuint	IdBuffer;

	struct FadeOutInfo
	{
		FadeOutInfo(): used(false), source(-1) {}
		int		source;
		bool	used;
	};
//----------------------------------------------------------//


class SoundManager
{
	public:		
		SoundManager();
		~SoundManager();

		//-----------GENERAL FUNCTIONS---------------------
		bool	LoadSound	( const std::string& file, const std::string & actionName );
		void	LoadSounds	( void );
		void	Reset		( void );
		void	Pause		( void );
		void	Stop		( void );
		void	SoundOn		( void ) {mSoundON=true;}
		void	SoundOff	( void ) {mSoundON=false;}
		void	SetGain		( float gain );
		float	GetGain		( void );

		void ToTurnUp(float amount); //--> Sube el volumen general
		void ToTurnDown(float amount); //--> Baja el volumen general
		void FadeOut(int source);
		void Update(float dt);
        
		//---------ACTION FUNCTIONS-------------------------------
		bool	PlayAction	( const std::string& action );
		bool	PlayAction	( const std::string& action, float posx, float posy, float posz);
		
	
		//-----SOURCE FUNCTIONS----------------------------
		int		CreateSource		( void );
		bool	DeleteSource		( int source );
        bool	PlaySource			( int source, const std::string& action, bool loop );
		bool	PlayRelativeSource	( int source, const std::string& action, bool loop );
		bool	PauseSource			( int source );
		bool	StopSource			( int source );
		bool	SetSourceGain		( int source, float inGain );
		bool	GetSourceGain		( int source, float& outGain );
		bool	SetSourcePosition	( int source, float posx, float posy, float posz);
		bool	GetSourcePosition	( int source, float& posx, float& posy, float& posz);
		bool	SetSourceVelocity	( int source, float velx, float vely, float velz);
		bool	GetSourceVelocity	( int source, float& velx, float& vely, float& velz);
		

		//-----LISTENER FUNCTIONS-------------------------
		void	SetListenerPosition		( float posx, float posy, float posz);
		void	GetListenerPosition		( float& posx, float& posy, float& posz);
		void	SetListenerVelocity		( float velx, float vely, float velz);
		void	GetListenerVelocity		( float& velx, float& vely, float& velz);
		
		void	SetListenerOrientation	(	float lookatx, float lookaty, float lookatz,
											float upx, float upy, float upz );

		void	GetListenerOrientation	(	float& lookatx, float& lookaty, float& lookatz,
											float& upx, float& upy, float& upz );
		
		
	private:
		bool	mSoundON;
		bool	mPause;
		
		std::map<Action, IdBuffer>		mBuffers;
		std::vector<InfoSource>			mSources;

		FadeOutInfo	mFadeOutSources[3];
		
	private:	
		bool			_initAL				( void );
		void			_finalizeAL			( void );
		bool			_loadSound			( const std::string& file, IdBuffer& buffer );
		void			_clear				( void );
		int				_getSource			( bool reserved=false );
		std::string		_getALErrorString	(ALenum err);
};

#endif //__SOUNDMANAGER_H
